package com.nd.android.u.ui.activity.message_chat;

import ims.utils.CommUtil;
import ims.utils.IMLogUtils;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;

import com.common.StarAppCIConst;
import com.nd.android.u.chatInterfaceImpl.ChatInterfaceImpl;
import com.nd.android.u.controller.ChatConst;
import com.nd.android.u.controller.ChatConst.KEY;
import com.nd.android.u.controller.ChatConst.LocalMessageType;
import com.nd.android.u.controller.MessageConst.GroupType;
import com.nd.android.u.ui.ChatEntry;
import com.nd.android.u.ui.widge.chatfragment.BottomFragment.CommonBottomListener;
import com.product.android.commonInterface.chat.ChatGroup;

import de.greenrobot.event.EventBus;

/**
 * 群组消息界面
 * <br>Created 2014-7-26 下午4:36:46
 * @version  
 * @author   cb		
 * @see 	 
 */
public class ChatActivity_Group extends BaseChatMessageActivity implements CommonBottomListener{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		EventBus.getDefault().register(this, View.class);
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this, View.class);
		super.onDestroy();
	}

	
	@Override
	protected boolean getParam(Bundle bundle) {
		messageType = LocalMessageType.GROUP;
		Object object=bundle.get(KEY.GID);
		generalId=CommUtil.parseLong(object.toString());
		if (generalId==0){
			return false;
		}
        if (bundle.containsKey(ChatConst.KEY.REPOST_DATA)){
			repostMsgId=bundle.getString(ChatConst.KEY.REPOST_DATA);
			if (TextUtils.isEmpty(repostMsgId)){
				IMLogUtils.e(IMLogUtils.CHAT,"get transmit key empty");
				return false;
			}
        }
        groupType = bundle.getInt(ChatConst.KEY.GROUPTYPE);
        if (TextUtils.isEmpty(name)){
    		//如果没有传联系人的名称过来，直接去取
    		getName();
    	}
        if (bundle.containsKey("stype")){
        	groupType = CommUtil.parseInt(bundle.getString("stype"));	
        }
		return true;
	}

    @Override
    public void rightBtnHandle() {
        Bundle bundle = new Bundle();
        switch (groupType) {
        case ChatGroup.GROUP_TYPE_NORMAL_UAP:
        case ChatGroup.GROUP_TYPE_NORMAL_OAP:
            bundle.putLong(ChatConst.KEY.GID, generalId);
            ChatEntry.INSTANCE.chatCallOtherModel_UI.gotoGroupManagerActivity(this, bundle);
            break;
        case GroupType.DISCUSSION:
            bundle.putLong(ChatConst.KEY.GID, generalId);
            ChatEntry.INSTANCE.chatCallOtherModel_UI.gotoDiscussionManagerActivity(this, bundle);
            break;
        case GroupType.DEPARTMENT:
            bundle.putLong(ChatConst.KEY.DEPTID, generalId);
            ChatEntry.INSTANCE.chatCallOtherModel_UI.gotoGroupManagerActivity(this, bundle);
            break;
        default:
            break;
        }
    }
    
    @Override
    protected void addDefaultFragment() {
    	super.addDefaultFragment();
    	initBottomView();
    }

    @Override
    public void onClickSwitchBtn() {

    }

    @Override
    public void createPopQuickReply() {
        mBottomFragment.dismisswidge();
        createPopQuickReply(generalId);
    }

    @Override
    public void showPopQuickReply(int width, int hight) {
        mPopQuickReply.show(findViewById(CONTENT_CONTAINER_ID), width, hight);
    }

	/**
	 * 获取名称（有可能别的界面没有传这个参数过来）
	 * <br>Created 2014-7-31 下午10:48:46
	 * @author  cb
	*/
	protected void getName() {
		name=ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getGroupName(groupType, generalId);
	}
	
	/**
	 * 重绘界面后的通知
	 * 
	 * <br>
	 * Created 2014-11-4 上午11:13:29
	 * 
	 * @param obj
	 * @author : HuangYK
	 */
	public void onEventMainThread(View view) {
		mMessageListFragment.scrollToBottom(true);
	}
	
	@Override
	protected void processOtherMessageNotify(final Message message) {
		if (message==null){
    		return;
    	}
    	switch (message.what) {
		case StarAppCIConst.CHAT_NOTIFY_GROUP_NAME_210003:
			if (message.obj==null){
				return;
			}
			Bundle bundle = message.getData();
            if(bundle != null){
                long gid = bundle.getLong(com.nd.android.u.controller.ChatConst.KEY.GID);
                if(gid == generalId){
                	runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
		                    mActivityTitle.setTitleText(message.obj.toString());
						}
					});
                }
            }
			break;

		default:
			break;
		}
	}
}
